package paiement;
import javax.swing.JFrame;
import java.awt.BorderLayout;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import java.awt.Cursor;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;





public class Contact_form extends JFrame {
	
	//declaration des variables
	public JTextField textNumCarte;
	public JTextField textDate;
	public JTextField textCcv;
	public JTextField textPrenom;
	public JTextField textNom;
	public JTextField textAdresse;
	public JTextField textVille;
	public JTextField textEtat;
	public JTextField textCode;
	public JTextField textPays;
	public JTextField textDevise;
	public JTextField textMontant;
	PaymentProcessor p;
	private String search;
	
	//le constructeur
	public Contact_form() {
		
		
		setResizable(false);
		
		
		setPreferredSize(new Dimension(5, 5));
		
		//icone de la fenetre
		setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Christelle\\Desktop\\47903599.gif"));
		//titre du formulaire
		setTitle("Paiement par carte bancaire");
		//definition du layout principal
		getContentPane().setLayout(new BorderLayout(0, 0));
	
		
		//creation d'un panel en position nord(en haut) du formulaire
		JPanel panel = new JPanel();
		panel.setBackground(new Color(255, 255, 255));
		panel.setToolTipText("");
		panel.setPreferredSize(new Dimension(10, 50));
		getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Informations personnelles");
		lblNewLabel.setFont(new Font("Book Antiqua", Font.PLAIN, 18));
		lblNewLabel.setForeground(new Color(75, 0, 130));
		lblNewLabel.setBounds(135, 11, 244, 28);
		panel.add(lblNewLabel);
		
		
		//panel contenant les boutons
		JPanel panel_2 = new JPanel();
		panel_2.setBackground(new Color(255, 255, 255));
		panel_2.setPreferredSize(new Dimension(10, 70));
		getContentPane().add(panel_2, BorderLayout.SOUTH);
		panel_2.setLayout(null);
		
		//le bouton BUY
		JButton btnBuy = new JButton("BUY");
		btnBuy.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				    //searchResult();
					PaymentProcessor p = new PaymentProcessor();
					
					p.setX_amount(textMontant.getText());
					p.setX_card_num(textNumCarte.getText());
					p.setX_address(textAdresse.getText());
					p.setX_exp_date(textDate.getText());
					p.setX_first_name(textPrenom.getText());
					p.setX_last_name(textNom.getText());
					p.setX_zip(textCode.getText());

					
					try {
						String r=p.doPayment();
						if (searchResult(r)){
							JOptionPane.showMessageDialog(null, "Traitement correctement effectue");
						}else{
							String [] responses = r.split("\\|");
							if (responses.length>=3){
							JOptionPane.showMessageDialog(null, "Traitement pas correctement effectue \n"+responses[3]);
							}else{

								JOptionPane.showMessageDialog(null, "Traitement pas correctement effectue");
							}
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						JOptionPane.showMessageDialog(null, e.getMessage());
					}	
			}	
			
		}
		);
		btnBuy.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		btnBuy.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		btnBuy.setIconTextGap(8);
		btnBuy.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnBuy.setBackground(new Color(230, 230, 250));
		btnBuy.setBounds(33, 9, 116, 38);
		panel_2.add(btnBuy);
		
		
		
		
		//le bouton annuler
		JButton btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				 textNumCarte.setText("");
				 textDate.setText("");
				 textCcv.setText("");
				 textPrenom.setText("");
				 textNom.setText("");
				 textAdresse.setText("");
				 textVille.setText("");
				 textEtat.setText("");
				 textCode.setText("");
				 textPays.setText("");
				 textDevise.setText("");
				 textMontant.setText("");
			}
		});
		btnAnnuler.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		btnAnnuler.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		btnAnnuler.setBackground(new Color(230, 230, 250));
		btnAnnuler.setFont(new Font("Tahoma", Font.PLAIN, 16));
		btnAnnuler.setBounds(186, 11, 110, 36);
		panel_2.add(btnAnnuler);
		
		//le bouton quitter
		JButton btnQuitter = new JButton("Quitter");
		btnQuitter.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				System.exit(0);
			}
		});
		btnQuitter.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
			}
		});
		btnQuitter.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		btnQuitter.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		btnQuitter.setBackground(new Color(230, 230, 250));
		btnQuitter.setFont(new Font("Tahoma", Font.PLAIN, 16));
		btnQuitter.setBounds(336, 11, 100, 36);
		panel_2.add(btnQuitter);
		
		//creation du 3eme panel(partie grise du formulaire)
		JPanel panel_3 = new JPanel();
		panel_3.setBackground(new Color(192, 192, 192));
		panel_3.setForeground(new Color(0, 0, 0));
		getContentPane().add(panel_3, BorderLayout.CENTER);
		panel_3.setLayout(null);
		
		
		
		JLabel lblCreditCardNumber = new JLabel("Credit Card Number");
		lblCreditCardNumber.setForeground(new Color(75, 0, 130));
		lblCreditCardNumber.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		lblCreditCardNumber.setBounds(10, 21, 142, 14);
		panel_3.add(lblCreditCardNumber);
		
		JLabel lblExp = new JLabel("Exp");
		lblExp.setForeground(new Color(75, 0, 130));
		lblExp.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		lblExp.setBounds(172, 21, 46, 14);
		panel_3.add(lblExp);
		
		JLabel lblCcv = new JLabel("CCV");
		lblCcv.setForeground(new Color(75, 0, 130));
		lblCcv.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		lblCcv.setBounds(322, 21, 46, 14);
		panel_3.add(lblCcv);
		
		textNumCarte = new JTextField();
		textNumCarte.setText("6011000000000012");
		textNumCarte.setBackground(new Color(255, 255, 255));
		textNumCarte.setCaretColor(new Color(0, 0, 0));
		textNumCarte.setBounds(10, 46, 142, 33);
		panel_3.add(textNumCarte);
		textNumCarte.setColumns(10);
		
		textDate = new JTextField();
		textDate.setText("04/17");
		textDate.setBounds(172, 46, 132, 33);
		panel_3.add(textDate);
		textDate.setColumns(10);
		
		textCcv = new JTextField();
		textCcv.setText("782");
		textCcv.setBounds(322, 46, 133, 33);
		panel_3.add(textCcv);
		textCcv.setColumns(10);
		
		JLabel lblFirstName = new JLabel("First Name");
		lblFirstName.setForeground(new Color(75, 0, 130));
		lblFirstName.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		lblFirstName.setBounds(10, 102, 78, 14);
		panel_3.add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setForeground(new Color(75, 0, 130));
		lblLastName.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		lblLastName.setBounds(172, 102, 70, 14);
		panel_3.add(lblLastName);
		
		textPrenom = new JTextField();
		textPrenom.setText("John");
		textPrenom.setBounds(10, 127, 142, 33);
		panel_3.add(textPrenom);
		textPrenom.setColumns(10);
		
		textNom = new JTextField();
		textNom.setText("Doe");
		textNom.setBounds(172, 127, 132, 33);
		panel_3.add(textNom);
		textNom.setColumns(10);
		
		JLabel lblAdresse = new JLabel("Adresse");
		lblAdresse.setForeground(new Color(75, 0, 130));
		lblAdresse.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		lblAdresse.setBounds(10, 176, 70, 14);
		panel_3.add(lblAdresse);
		
		JLabel lblCity = new JLabel("City");
		lblCity.setForeground(new Color(75, 0, 130));
		lblCity.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		lblCity.setBounds(172, 174, 46, 19);
		panel_3.add(lblCity);
		
		textAdresse = new JTextField();
		textAdresse.setText("123 Main Street");
		textAdresse.setBounds(10, 195, 142, 33);
		panel_3.add(textAdresse);
		textAdresse.setColumns(10);
		
		textVille = new JTextField();
		textVille.setText("Boston");
		textVille.setBounds(172, 196, 132, 32);
		panel_3.add(textVille);
		textVille.setColumns(10);
		
		JLabel lblState = new JLabel("State");
		lblState.setForeground(new Color(75, 0, 130));
		lblState.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		lblState.setBounds(10, 246, 46, 14);
		panel_3.add(lblState);
		
		JLabel lblZipCode = new JLabel("Zip Code");
		lblZipCode.setForeground(new Color(75, 0, 130));
		lblZipCode.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		lblZipCode.setBounds(172, 246, 70, 14);
		panel_3.add(lblZipCode);
		
		JLabel lblCountry = new JLabel("Country");
		lblCountry.setForeground(new Color(75, 0, 130));
		lblCountry.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		lblCountry.setBounds(322, 246, 61, 14);
		panel_3.add(lblCountry);
		
		textEtat = new JTextField();
		textEtat.setText("MA");
		textEtat.setBounds(10, 271, 142, 33);
		panel_3.add(textEtat);
		textEtat.setColumns(10);
		
		textCode = new JTextField();
		textCode.setText("02142");
		textCode.setBounds(172, 271, 132, 33);
		panel_3.add(textCode);
		textCode.setColumns(10);
		
		textPays = new JTextField();
		textPays.setText("US");
		textPays.setBounds(322, 271, 133, 33);
		panel_3.add(textPays);
		textPays.setColumns(10);
		
		textDevise = new JTextField();
		textDevise.setText("$ USA");
		textDevise.setBounds(322, 127, 133, 33);
		panel_3.add(textDevise);
		textDevise.setColumns(10);
		
		textMontant = new JTextField();
		textMontant.setText("5000");
		textMontant.setBounds(322, 201, 133, 27);
		panel_3.add(textMontant);
		textMontant.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Currency");
		lblNewLabel_1.setForeground(new Color(75, 0, 130));
		lblNewLabel_1.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		lblNewLabel_1.setBounds(322, 103, 85, 14);
		panel_3.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("Amount");
		lblNewLabel_2.setFont(new Font("Book Antiqua", Font.PLAIN, 14));
		lblNewLabel_2.setForeground(new Color(75, 0, 130));
		lblNewLabel_2.setBounds(322, 177, 85, 14);
		panel_3.add(lblNewLabel_2);
	}
	
	//voici une ligne qui explique cherche l'etat du paiemt a partir de la reponse qu'on recoit
	
	//methode qui me casse la tete
		public	boolean searchResult(String motif,String chaine) {
			 this.search = motif;
			// String result = "le paiement a reussi";
			// result="This transaction has been approved.";
			 
			 Pattern pattern = Pattern.compile(search);
			 Matcher  matcher = pattern.matcher(chaine);
			 
			 if(matcher.find()){
			  System.out.print("trouver ici");
			  return true;
			 }else{
			  System.out.print("pas trouve ici");
			  return false;
			 }
			// boolean etatDuPaiement = this.SearchResult("reussi");
			}
		public	boolean searchResult(String chaine) {
			String motif="This transaction has been approved.";
			 return this.searchResult(motif,chaine);
		}

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		Contact_form cf=new Contact_form();
		String msg="le paiement a reussi ";
		boolean r=cf.searchResult(msg);
		cf.setSize(500, 500);
		cf.setLocation(400, 150);
		cf.setVisible(true);
		
		
	}

	
	}



